<template>
  <div class="order-list">
    <h2>地址列表</h2>
    <div class="actions">
      <button class="btn btn-add" @click="showAddressModal = true">新增收货地址</button>
    </div>

    <!-- 地址弹窗组件 -->
    <AddressModal
        :visible="showAddressModal"
        @close="showAddressModal = false"
        @saved="handleAddressSaved"
    />

    <table class="styled-table">
      <thead>
      <tr>
        <th>地址ID</th>
        <th>地址区域</th>
        <th>详细地址</th>
        <th>电话</th>
        <th>联系人</th>
        <th>操作</th>
      </tr>
      </thead>
      <tbody>
      <template v-for="(address, index) in responseData" :key="index">
        <tr v-if="address.addressIsExist === 1">
          <td>{{ address.addressId }}</td>
          <td>{{ address.addressArea }}</td>
          <td>{{ address.addressDetail }}</td>
          <td>{{ address.addressPhone }}</td>
          <td>{{ address.addressLinkman }}</td>
          <td>
            <button class="btn btn-delete" @click="deleteAddress(address.addressId)">
              删除地址
            </button>
            <button @click="openEditModal(address.addressId)">编辑</button>

          </td>
        </tr>
        <EditAddressModal
            :visible="showEditModal"
            :addressId="currentAddressId"
            @close="showEditModal = false"
            @saved="refreshAddressList"
        />
      </template>
      </tbody>
    </table>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useUserStore } from '@/stores/user';
import { BASE_URL } from "@/util/Config.js";
import AddressModal from '@/components/AddressModal.vue';
import EditAddressModal from '@/components/EditAddressModal.vue';
export default {
  components: {
    AddressModal,
    EditAddressModal
  },
  setup() {
    const userStore = useUserStore();
    const showAddressModal = ref(false);
    return {
      userStore,
      showAddressModal
    };
  },
  data() {
    return {
      responseData: [],
      showEditModal: false,
      currentAddressId: null
    };
  },
  methods: {
    async sendData() {
      try {
        const response = await fetch(BASE_URL + '/my/addresses', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ userId: this.userStore.userInfo?.userId })
        });

        const result = await response.json();
        this.responseData = result.data || [];
      } catch (error) {
        console.error('获取地址失败:', error);
      }
    },

    async deleteAddress(addressId) {
      if (!confirm('确定要删除该地址吗？')) return;

      try {
        await fetch(BASE_URL + '/my/deleteAddress', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ addressId })
        });
        this.sendData(); // 刷新列表
      } catch (error) {
        console.error('删除地址失败:', error);
      }
    },
    openEditModal(addressId) {
      this.currentAddressId = addressId;
      this.showEditModal = true;
    },
    refreshAddressList() {
      // 重新加载地址列表
      this.loadAddresses();
    },
    loadAddresses() {
      this.sendData();
    },
    handleAddressSaved() {
      this.showAddressModal = false;
      this.sendData(); // 保存成功后刷新列表
    }
  },
  mounted() {
    this.sendData();
  }
}
</script>

<style scoped>
.order-list {
  padding: 20px;
}

.styled-table {
  width: 100%;
  border-collapse: collapse;
  margin: 20px 0;
  background-color: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  overflow: hidden;
}

.styled-table th,
.styled-table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #ddd;
}

.styled-table thead tr {
  background-color: #1abc9c;
  color: white;
}

.actions {
  margin-bottom: 20px;
  text-align: right;
}

.btn {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
}

.btn-add {
  background-color: #3498db;
  color: white;
}

.btn-add:hover {
  background-color: #2980b9;
  transform: translateY(-1px);
}

.btn-delete {
  background-color: #e74c3c;
  color: white;
}

.btn-delete:hover {
  background-color: #c0392b;
  transform: translateY(-1px);
}
</style>